package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/v1/channel")
public class WmchannelController {

    @Autowired
    private WmChannelService wmChannelService;


    @GetMapping("/channels")
    public ResponseResult findAll(){
        return wmChannelService.findAll();
    }

    /**
     * 频道分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public PageResponseResult channelList(@RequestBody ChannelDto dto){

        Page wmChannelPage = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(dto.getName()), WmChannel::getName, dto.getName());
        wrapper.orderByAsc(WmChannel::getOrd);

        Page<WmChannel> wmChannelPage1 = wmChannelService.page(wmChannelPage, wrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) wmChannelPage1.getTotal());
        pageResponseResult.setData(wmChannelPage1.getRecords());
        return pageResponseResult;
    }

    /**
     * 添加频道
     * @param adChannel
     * @return
     */
    @PostMapping("/save")
    public ResponseResult channelSave(@RequestBody AdChannel adChannel){
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannel,wmChannel);
        wmChannel.setCreatedTime(new Date());
        wmChannelService.save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @PostMapping("/update")
    public ResponseResult channelUpate(@RequestBody AdChannel adChannel){
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannel,wmChannel);
        wmChannel.setCreatedTime(new Date());
        wmChannelService.updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @GetMapping("/del/{id}")
    public ResponseResult SensitiveUpate(@PathVariable Long id){
        wmChannelService.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}